﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Approval.Operations;
    using UFIDA.U9.Approval.Operations.Proxy;
    using UFIDA.U9.Base.UserRole;
    using UFIDA.U9.CBO.SCM.ProjectTask;
    using UFIDA.U9.PM.PM_Util;
    using UFIDA.U9.SM.SO;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Util.Data;

    /// <summary>
    /// FlowApproveSV partial 
    /// </summary>	
	public partial class FlowApproveSV 
	{	
		internal BaseStrategy Select()
		{
			return new FlowApproveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class FlowApproveSVImpementStrategy : BaseStrategy
	{
		public FlowApproveSVImpementStrategy() { }

		public override object Do(object obj)
		{
			FlowApproveSV bpObj = (FlowApproveSV)obj;

			try
			{
                if (bpObj == null || bpObj.Data == null || bpObj.Data.Count == 0)
				{
					throw new Exception("传入参数为空");
                }
                //构造数据
                List<BatchApproveActionDTOData> actions = new List<BatchApproveActionDTOData>();
                User user = null;
                foreach (var indto in bpObj.Data)
                {
                    //entity_id和entity_type 就是第一步查询出来的数据
                    if (string.IsNullOrEmpty(indto.EntityName)) throw new Exception("EntityName 为空");
                    //if (string.IsNullOrEmpty(indto.EntityNo)) throw new Exception("EntityNo 为空");
                    BusinessEntity entity = null;
                    if (indto.EntityID > 0)
                    {
                        EntityQuery qry = new EntityQuery(indto.EntityName);
                        entity = (BusinessEntity)qry.FindByID(indto.EntityID);
                    }
                    else if (!string.IsNullOrEmpty(indto.EntityNo))
                    {
                        entity = U9Utils.DocQuery(indto.EntityName, indto.EntityNo);
                    }
                    if (entity == null)
                    {
                        throw new Exception($"单据查询失败,实体类型[{indto.EntityName}],单号:[{indto.EntityNo}]");
                    }
                    BusinessEntity.EntityKey entityKey = entity.Key;
                    BatchApproveActionDTOData actdto = new BatchApproveActionDTOData();
                    actdto.EntityID = entityKey;
                    //审批动作int : 同意=1 拒绝=2 退回=3
                    actdto.Action = indto.Action;
                    actdto.Opinion = indto.Opinion;      //审批意见string  
                    if (indto.Action == 8 && string.IsNullOrEmpty(indto.UserList))
                    { 
                        //当转发时,设置用户id.
                        actdto.UserList = indto.UserList;
                    }
                    actdto.ApproveType = 3;//工作流审批
                    //操作者
                    user = EntityUtils.GetEntityByCode<User>(indto.OperatorCode);
                    actdto.OperatorId = user.ID; 
                    actions.Add(actdto);
                }
                //更新上下文
                UFIDA.U9.CBO.Pub.Controller.ContextDTOData contextDTOData = U9Utils.CreateContext();
                contextDTOData.UserCode = user.Code;
                contextDTOData.UserID = user.ID;
                U9Utils.SetContext(contextDTOData);
                //调用BP
                BatchApproveBPProxy batchBp = new BatchApproveBPProxy();
                batchBp.ApproveActions = actions;
                batchBp.IsSerial = false;
                List<BatchApproveResultData> approvalResults = batchBp.Do();
                //处理返回
                if (!approvalResults[0].IsSuccessful)
                {
                    throw new Exception(approvalResults[0].ApproveDetail);
                }
                return ResultUtils.success(bpObj, bpObj.Data, approvalResults);
            }
            catch (Exception ex)
			{
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
			
		}		
	}

	#endregion
	
	
}